﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using Model.View;
using BLL;
using Model;
using POS.Framework;
using DevExpress.Utils;
using DevExpress.Data.Filtering;

namespace POS.ModForm.Report
{
    public partial class frmStock : DevExpress.XtraEditors.XtraForm
    {
        public frmStock()
        {
            InitializeComponent();
        }

        private void frmStock_Load(object sender, EventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                this._ProductID.SelectedIndex = 0;
                this.gridView1.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
                this.gridView2.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(SpecialGridView.DrawRowIndicator);
                SetButtonClickEvent();
                SearchData();
            }
        }
        void SetButtonClickEvent()
        {
            this.listButtonUC.IsHide(new List<string>{"btnNew"});
            this.listButtonUC.Query.Click += new EventHandler(Query_Click);
        }
        void Query_Click(object sender,EventArgs e) 
        {
            using (WaitDialogForm wdf = new WaitDialogForm(MsgBox.WaitLoading))
            {
                SearchData();
                wdf.Close();
            }
        }
        void SearchData()
        {
            XPView xpView = new XPView(DatabaseSchema.Session, typeof(StockSummary));
            StockSummaryView ssv = new StockSummaryView(xpView);

            GroupOperator goFilter = new GroupOperator();
            if (!string.IsNullOrEmpty(this._ProductCode.Text.Trim()))
            {
                if (this._ProductID.SelectedIndex == 0)
                    goFilter.Operands.Add(CriteriaOperator.Parse("ProductID.ProductCode=?", this._ProductCode.Text.Trim()));
                if (this._ProductID.SelectedIndex == 1)
                    goFilter.Operands.Add(CriteriaOperator.Parse("ProductID.ProductName=?", this._ProductCode.Text.Trim()));
                if (this._ProductID.SelectedIndex == 2)
                    goFilter.Operands.Add(CriteriaOperator.Parse("ProductID.ProductSpec=?", this._ProductCode.Text.Trim()));
            }
            if (!string.IsNullOrEmpty(this._BrandName.Text.Trim())) 
                goFilter.Operands.Add(CriteriaOperator.Parse(string.Format("ProductID.BrandID.BrandName like '%{0}%'", this._BrandName.Text.Trim())));
            if (!string.IsNullOrEmpty(this._CategoryName.Text.Trim()))
                goFilter.Operands.Add(CriteriaOperator.Parse(string.Format("ProductID.CategoryID.CategoryName like '%{0}%'", this._CategoryName.Text.Trim())));
            xpView.Criteria = goFilter;
            this._gcStockSummary.DataSource = xpView;
            SpecialGridView.SetGridViewIndicatorWidth(xpView.Count, this.gridView2);

            XPCollection<TS_StockDetail> sd = new DevExpress.Xpo.XPCollection<TS_StockDetail>(DatabaseSchema.Session, goFilter);
            this._gcStockDetail.DataSource = sd;
            SpecialGridView.SetGridViewIndicatorWidth(sd.Count, this.gridView1);
        }
    }
}